package cn.starrysky108.simpleerp.core.base;

import cn.starrysky108.simpleerp.core.enums.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author Carl Lee
 * HTTP统一响应结果定义，构造器私有化，外部不能创建，
 */
@Data
public class BaseResponseEntity<T> implements Serializable {
    /**
     * 是否响应成功
     */
    private Boolean success;
    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 响应数据
     */
    private String message;

    private BaseResponseEntity(Boolean success, ResultCodeEnum resultCodeEnum, T data) {
        this.success = success;
        this.message = resultCodeEnum.getMessage();
        this.code = resultCodeEnum.getCode();
        this.data = data;
    }

    private BaseResponseEntity(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }
    /**
     * 响应成功，带响应数据
     *
     * @param data 数据
     */
    public static <T> BaseResponseEntity<T> success(T data) {
        return new BaseResponseEntity<>(true, ResultCodeEnum.SUCCESS, data);
    }

    /**
     * 响应成功，带响应数据
     *
     * @param data 数据
     */
    public static <T> BaseResponseEntity<T> success(String message, T data) {
        return new BaseResponseEntity<>(true, ResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    /**
     * 响应成功，响应码由ResultCodeEnum定义（推荐）
     *
     * @param resultCodeEnum 响应状态枚举
     * @param data           响应数据
     * @param <T>            响应数据类型
     */
    public static <T> BaseResponseEntity<T> success(ResultCodeEnum resultCodeEnum, T data) {
        return new BaseResponseEntity<>(true, resultCodeEnum, data);
    }

    /**
     * 响应成功，自定义响应码与响应信息
     *
     * @param code    响应状态码
     * @param message 响应状态信息
     * @param data    响应数据
     * @param <T>     响应数据类型
     */
    public static <T> BaseResponseEntity<T> success(Integer code, String message, T data) {
        return new BaseResponseEntity<>(true, code, message, data);
    }

    /**
     * 成功响应,带信息与code
     *
     * @param message 响应信息
     */
    public static <T> BaseResponseEntity<T> success(Integer code, String message) {
        return new BaseResponseEntity<>(true, code, message, null);
    }

    /**
     * 响应失败，带响应状态枚举
     *
     * @param resultCodeEnum 响应状态枚举
     */
    public static <T> BaseResponseEntity<T> failure(ResultCodeEnum resultCodeEnum) {
        return new BaseResponseEntity<>(false, resultCodeEnum, null);
    }

    /**
     * 失败响应,带错误信息，默认响应码为ResultCodeEnum.FAIL=-1
     *
     * @param message 响应错误信息
     */
    public static <T> BaseResponseEntity<T> failure(String message) {
        return new BaseResponseEntity<>(false, ResultCodeEnum.FAIL.getCode(), message, null);
    }

    /**
     * 失败响应,带错误信息与code
     *
     * @param message 响应错误信息
     */
    public static <T> BaseResponseEntity<T> failure(Integer code, String message) {
        return new BaseResponseEntity<>(false, code, message, null);
    }


    /**
     * 响应失败,带响应状态枚举与响应数据
     *
     * @param resultCodeEnum 响应状态枚举
     * @param data           错误数据
     */
    public static <T> BaseResponseEntity<T> failure(ResultCodeEnum resultCodeEnum, T data) {
        return new BaseResponseEntity<>(false, resultCodeEnum, data);
    }

    /**
     * 响应失败，自定义响应结果
     *
     * @param code    响应码
     * @param message 响应信息
     * @param data    响应数据
     * @param <T>     响应数据类型
     */
    public static <T> BaseResponseEntity<T> failure(Integer code, String message, T data) {
        return new BaseResponseEntity<>(false, code, message, data);
    }
}
